﻿using Traceability.Register.ErrorCode.Result;

namespace Traceability.Register.ErrorCode.Result;

/// <summary>
/// 公共返回类
/// </summary>
public class ApiResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool IsSuc { get;set; }
    /// <summary>
    /// 消息提示语
    /// </summary>
    public string Msg { get; set; }
    /// <summary>
    /// 数据
    /// </summary>
    public object? Data { get; set; }
    /// <summary>
    /// 状态码
    /// </summary>
    public ApiResultCode Code { get; set; }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="isSuc"></param>
    /// <param name="msg"></param>
    /// <param name="data"></param>
    /// <param name="code"></param>
    private ApiResult(bool isSuc,string msg,object? data,ApiResultCode code)
    {
        IsSuc = isSuc;
        Msg = msg;
        Data = data;
        Code = code;    
    }
    
    /// <summary>
    /// 成功返回
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="data"></param>
    /// <returns></returns>
    public static ApiResult Ok(object? data=null,string msg="操作成功")
    {
        return new ApiResult(true, msg, data, ApiResultCode.Success);
    }   

    /// <summary>
    /// 失败返回
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static ApiResult Fail(string msg="操作失败")
    {
        return new ApiResult(false, msg, null, ApiResultCode.Fail);
    }
}

